--[[
	Authors: 
		______    _     __          ______   _  __
	   / ____/   (_)   / /  ___    / ____/  | |/ /
	  / /_      / /   / /  / _ \  / __/     |   / 
	 / __/     / /   / /  /  __/ / /___    /   |  
	/_/       /_/   /_/   \___/ /_____/   /_/|_|  
						&
  _  __                _        ____                _           
 | |/ /_ __ ___  _ __ (_)_  __ / ___|___   ___   __| | ___ _ __ 
 | ' /| '__/ _ \| '_ \| \ \/ /| |   / _ \ / _ \ / _` |/ _ \ '__|
 | . \| | | (_) | | | | |>  < | |__| (_) | (_) | (_| |  __/ |   
 |_|\_\_|  \___/|_| |_|_/_/\_(_)____\___/ \___/ \__,_|\___|_|   

]]

local hashstring = get('model_hash') or ' ţô|÷ą^Ż“źŇmţ‘|“Š‹®ĺ‘Ö¤ŤŤÖ—Ľ°' -- global settings.xml

addEvent("queryDataDecode",true)
addEventHandler("queryDataDecode",resourceRoot,function()
	triggerLatentClientEvent(client,"dataDecode",client,hashstring)
end)


addEventHandler("onResourceStart",resourceRoot,function()
	local isAnyReplaceFileDownload = false
	local xmlroot =  XML.load("meta.xml",true)
	
	
	--#time          \t*\-\-\#time.+?\-\-\#endtime\n     <- regex/g aby wyczyscic kod ze smieci :) 
	local timeConvertionAll = getTickCount()
	--#endtime
	
	for _,xtv in pairs( xmlroot.children ) do
		
		--#time
		local timeConvertion = getTickCount()
		--#endtime
		if xtv.name == "file" then
			
			local fpath = xtv:getAttribute("src")
			local hinputfile = File( fpath )
			
			if hinputfile and not hinputfile:read(hinputfile.size):find(hashstring) then
				isAnyReplaceFileDownload = true
				hinputfile.pos = 0
				
				local posInsert = math.random(64,hinputfile.size - 64)
				local beforeRaw = hinputfile:read(posInsert)
				local afterRaw = hinputfile:read(hinputfile.size - posInsert)
				
				hinputfile.pos = 0
				if hinputfile:write(beforeRaw,hashstring,afterRaw) and hinputfile:flush() then
				end
			else
			end
			hinputfile:close()
		end
	end
	
	--#time
	--#endtime
	xmlroot:unload()
	
	if isAnyReplaceFileDownload then
		--#time
		--#endtime
		restartResource(getThisResource())
	end
	
end)
